package com.fengkai.filepost.pdfoutstream.test.common.mq.producer;

import io.netty.channel.DefaultChannelId;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

/**
 * @author Fengkai
 * @creat 2021/12/28
 */
public class SyncProducer {
    //同步生产者
       public static void main(String[] args) throws Exception {
           DefaultMQProducer pg = new DefaultMQProducer("pg");
           pg.setNamesrvAddr("192.168.157.129:9876");
           pg.setRetryTimesWhenSendFailed(3);
           DefaultChannelId.newInstance();
           pg.start();

           for (int i = 0; i <1000 ; i++) {
               byte[] body = ("HI," + "someTopic：" +i).getBytes();
               Message message = new Message("someTopic", "someTag", body);
               //为消息指定key
               message.setKeys("key-" + i );
               SendResult sendResult = pg.send(message);
               System.out.println(sendResult);
           }
            pg.shutdown();
       }
}
